<?PHP
  class SettingsForm{
    public $area     = "global";
    public $areaType = "global";
	public $role     = null;
	public $url      = "";
	
	public function display(){
      if($_POST['save']){
        foreach($_POST as $property=>$value){
	      if($property != "save" && $property != "roles"){
		    setSetting($this->area,$this->areaType,$property,$value,$this->role);
	      }
	    }
	  }
	  $template = new Template();
	  $template->load("form_settings");
		
      $roleselector = "<select name=\"roles\" onchange=\"document.location.href='".$this->url."&role=' + this.options[this.selectedIndex].value;\">";
		
      $roles = $GLOBALS['db']->ReadRows("SELECT * FROM {'dbprefix'}roles ORDER BY name");
      if($roles){
        foreach($roles as $role){
		  if($this->role == $role->id){
		    $roleselector .= "<option value=\"".$role->id."\" selected=\"selected\">".$role->name."</option>";
		  }
          else{
		    $roleselector .= "<option value=\"".$role->id."\">".$role->name."</option>";
		  }
	    }
	  }
		
      $roleselector .= "</select>";
	  $template->assign_var("ROLES",$roleselector);
	  $template->assign_var("URL",$this->url."&role=".$this->role);

      $sql = "SELECT DISTINCT * FROM {'dbprefix'}settings WHERE area = '".$this->area."' AND areaType = '".$this->areaType."' AND activated = 1";
      if($this->role){
  	    $sql .= " AND role = '".$this->role."'";
			if($this->role != 3){
				$sql .=" UNION SELECT DISTINCT * FROM {'dbprefix'}settings WHERE area = '".$this->area."' AND areaType = '".$this->areaType."' AND activated = 1 AND role = '3' AND property NOT IN (SELECT DISTINCT property FROM {'dbprefix'}settings WHERE area = '".$this->area."' AND areaType = '".$this->areaType."' AND activated = 1 AND role = '".$this->role."')";
			}
      }
      $rows = $GLOBALS['db']->ReadRows($sql);
	  if($rows){
        foreach($rows as $row){
		  $index = $template->add_loop_item("SETTINGS");
		  $template->assign_loop_var("SETTINGS", $index, "PROPERTY", $row->property);
		  $template->assign_loop_var("SETTINGS", $index, "DESCRIPTION",$row->description);
		  $control        = new $row->type;
	      $control->name  = $row->property;
	      $control->value = $row->value;
  		  $template->assign_loop_var("SETTINGS", $index, "CONTROL",$control->getCode());
        }
      }
	  $template->output();
	}
  }
?>